package cn.com.anypay.manager.controller.sdk;

import cn.com.anypay.manager.service.SmsService;
import cn.com.anysdk.sms.exception.SmsException;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/api/sms")
public class SmsController {
    private static final Logger logger = LoggerFactory.getLogger(SmsController.class);

    @Resource(name = "smsServiceImpl")
    private SmsService smsService;

    @GetMapping("/config")
    public ResponseEntity<Map<String, Object>> getConfig() {
        logger.info("开始获取短信配置");
        try {
            Map<String, Object> config = smsService.getCurrentConfig();
            logger.info("获取短信配置成功: {}", config);
            return ResponseEntity.ok(config);
        } catch (SmsException e) {
            logger.error("获取短信配置失败", e);
            return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/switch")
    public ResponseEntity<Map<String, Object>> switchProvider(@RequestBody Map<String, Object> config) {
        logger.info("开始切换短信服务商，配置参数: {}", config);
        try {
            String result = smsService.switchProvider(config);
            logger.info("切换短信服务商成功: {}", result);
            return ResponseEntity.ok(Map.of(
                "success", true,
                "message", result
            ));
        } catch (SmsException e) {
            logger.error("切换短信服务商失败", e);
            return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "message", e.getMessage()
            ));
        }
    }

    @PostMapping("/send")
    public ResponseEntity<Map<String, Object>> send(@RequestBody Map<String, String> request) {
        logger.info("收到发送短信请求，参数: {}", request);
        try {
            // 检查服务商配置
            Map<String, Object> currentConfig = smsService.getCurrentConfig();
            if (currentConfig == null || currentConfig.isEmpty()) {
                logger.warn("短信服务商未配置");
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "请先配置短信服务商"
                ));
            }

            String phone = request.get("phone");
            String templateParams = request.get("templateParams");

            logger.info("解析请求参数 - 手机号: {}, 模板参数: {}", phone, templateParams);

            if (phone == null || phone.isEmpty()) {
                logger.warn("手机号为空");
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "手机号不能为空"
                ));
            }

            if (templateParams == null || templateParams.isEmpty()) {
                logger.warn("模板参数为空");
                return ResponseEntity.badRequest().body(Map.of(
                    "success", false,
                    "message", "模板参数不能为空"
                ));
            }

            logger.info("开始发送短信 - 手机号: {}, 模板参数: {}", phone, templateParams);
            Map<String, Object> result = smsService.sendSms(phone, templateParams);
            logger.info("短信发送成功，结果: {}", result);
            return ResponseEntity.ok(result);
        } catch (SmsException e) {
            logger.error("发送短信失败", e);
            return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "message", e.getMessage()
            ));
        } catch (Exception e) {
            logger.error("发送短信时发生未知错误", e);
            return ResponseEntity.status(500).body(Map.of(
                "success", false,
                "message", "发送短信时发生未知错误: " + e.getMessage()
            ));
        }
    }
}
